package com.yunyao.dao.service.bamu.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yunyao.common.constant.enums.DataDeletedStatus;
import com.yunyao.common.constant.enums.OrderStatus;
import com.yunyao.common.core.BaseContext;
import com.yunyao.common.dto.order.SupplyOrderDto;
import com.yunyao.common.web.util.StringUtils;
import com.yunyao.dao.mapper.BamuSupplyApplyMapper;
import com.yunyao.dao.model.BamuSupplyApply;
import com.yunyao.dao.service.bamu.IBamuSupplyApplyService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 * 供货订单表 服务实现类
 * </p>
 *
 * @author chenyejian
 * @since 2024-07-02
 */
@Service
public class BamuSupplyApplyServiceImpl extends ServiceImpl<BamuSupplyApplyMapper, BamuSupplyApply> implements IBamuSupplyApplyService {

    @Autowired
    BamuSupplyApplyMapper bamuSupplyApplyMapper;
    /**
     * 根据供应订单查询条件获取供应申请列表
     *
     * 此方法接收一个供应订单查询条件对象（SupplyOrderDto），通过调用映射接口（bamuSupplyApplyMapper）的相应方法，
     * 实现从数据库中查询与给定条件匹配的供应申请列表并返回
     *
     * @param orderDto 供应订单查询条件对象，用于指定查询条件
     * @return 返回匹配的供应申请列表，类型为List<BamuSupplyApply>
     */
    @Override
    public List<BamuSupplyApply> getSupplyApplyList(SupplyOrderDto orderDto) {
        if (StringUtils.isNotEmpty(orderDto.getAuditStatus())) {
            String auditStateDescription = OrderStatus.getDescriptionByType(orderDto.getAuditStatus());
            String auditStateString = OrderStatus.getTypeStringByDesc(auditStateDescription);
            orderDto.setAuditStatus(auditStateString);
        }
        orderDto.setProjectIds(BaseContext.getProjectList());
        orderDto.setSupplierIds(BaseContext.getSupplierList());
        orderDto.setTenantId(BaseContext.getUserInfo().getTenantId());
        return bamuSupplyApplyMapper.getSupplyApplyList(orderDto);
    }

    /**
     * 根据采购申请ID获取供应申请信息
     * 此方法解释了为什么需要根据采购申请ID和未删除状态来查询供应申请信息
     *
     * @param purchaseApplyId 采购申请ID，用于定位具体的供应申请
     * @return 返回找到的供应申请信息，如果没有找到则返回null
     */
    @Override
    public BamuSupplyApply getByPurchaseApplyId(String purchaseApplyId) {
        // 创建查询条件，用于精确匹配供应申请ID和未被删除的记录
        LambdaQueryWrapper<BamuSupplyApply> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(BamuSupplyApply::getApplyId, purchaseApplyId).eq(BamuSupplyApply::getIsDeleted, DataDeletedStatus.NO.getType());
        // 使用查询条件获取单个实体，解释了这是为了获取具体的供应申请信息
        return list(queryWrapper).stream().findFirst().orElse(null);
    }


    @Override
    public List<BamuSupplyApply> listById(String Id) {
        // 检查合同ID是否为空或空白，如果是，则返回一个空列表
        if (org.apache.commons.lang3.StringUtils.isBlank(Id)) {
            return List.of();
        }
        // 创建一个LambdaQueryWrapper用于构建查询条件
        LambdaQueryWrapper<BamuSupplyApply> wrapper =
                new LambdaQueryWrapper<BamuSupplyApply>().eq(BamuSupplyApply::getId,
                        Id).eq(BamuSupplyApply::getIsDeleted, DataDeletedStatus.NO.getType());
        // 使用构建的查询条件获取并返回关联的材料列表
        return list(wrapper);
    }

    @Override
    public IPage<BamuSupplyApply> pageListSupplyApply(SupplyOrderDto orderDto) {
        if (StringUtils.isNotEmpty(orderDto.getAuditStatus())) {
            String auditStateDescription = OrderStatus.getDescriptionByType(orderDto.getAuditStatus());
            String auditStateString = OrderStatus.getTypeStringByDesc(auditStateDescription);
            orderDto.setAuditStatus(auditStateString);
        }
        orderDto.setProjectIds(BaseContext.getProjectList());
        orderDto.setSupplierIds(BaseContext.getSupplierList());
        orderDto.setTenantId(BaseContext.getUserInfo().getTenantId());
        long pageCount = bamuSupplyApplyMapper.pageCount(orderDto);
        // 创建并设置分页对象的属性
        IPage<BamuSupplyApply> page = new Page<>(orderDto.getPageIndex(), orderDto.getPageSize(), pageCount);
        // 如果总页数为0，则直接返回空的分页对象
        if (pageCount == 0) {
            return new Page<>(orderDto.getPageIndex(), orderDto.getPageSize(), 0);
        }
        // 计算当前页码对应的数据起始索引
        long index = orderDto.getPageIndex() <= 1 ? 0 : (orderDto.getPageIndex() - 1) * orderDto.getPageSize();
        orderDto.setPageIndex(index);
        List<BamuSupplyApply> supplyApplyList = bamuSupplyApplyMapper.pageListSupplyApply(orderDto);
        // 创建并设置分页对象的属性
        page.setRecords(supplyApplyList);
        // 计算总页数并设置
        page.setPages(pageCount % orderDto.getPageSize() == 0 ? pageCount / orderDto.getPageSize() : pageCount / orderDto.getPageSize() + 1);
        return page;
    }
}
